package com.mobilesales.android.activities.employees;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.ModelActivity;
import com.mobilesales.android.adapters.EmployeeAdapter;
import com.mobilesales.android.database.EmployeeManager;
import com.mobilesales.android.infrastructure.SyncItemTask;
import com.mobilesales.android.model.employees.Employee;
import com.mobilesales.android.model.employees.Message;
import com.mobilesales.android.model.exceptions.ServiceUnavailableException;
import com.mobilesales.android.services.EmployeeServiceClient;
import com.mobilesales.android.util.LogFlags;
import com.mobilesales.android.util.MessageBox;
import com.mobilesales.android.util.Settings;

public class MessageSend extends ModelActivity<Message> {

	private Spinner recipientSpinner;
	private EditText subjectField;
	private EditText contentField;

	private EmployeeAdapter recipientAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_compose);

		recipientSpinner = (Spinner) this.findViewById(R.id.recipientSpinner);
		subjectField = (EditText) this.findViewById(R.id.subjectTxt);
		contentField = (EditText) this.findViewById(R.id.contentTxt);

		this.loadItems();
	}

	public void send(View v) {
		if (validate()) {
			Message msg = new Message();
			msg.Content = contentField.getText().toString();
			msg.Subject = subjectField.getText().toString();
			Employee recipient = (Employee) recipientSpinner.getSelectedItem();
			msg.RecepientId = recipient.Id;

			super.setModel(msg);
			if (send(msg)) {
				MessageBox.show(this,
						getString(R.string.MessageSentSuccessfully));
				this.setResult(Activity.RESULT_OK);
				finish();
			} else {
				MessageBox.show(this, getString(R.string.ErrorMessageSend));
			}
		} else {
			MessageBox.show(this, getString(R.string.ErrorValidate));
		}
	}

	private boolean send(Message msg) {
		try {
			EmployeeServiceClient cli = new EmployeeServiceClient(
					Settings.EmployeeService());
			Settings s = Settings.getSettings();
			int senderId = s.getEmployeeId();
			cli.sendMessage(senderId, msg.RecepientId, msg.Subject, msg.Content);
			return true;
		} catch (ServiceUnavailableException e) {
			Log.e(LogFlags.RestService, e.getMessage(), e);
		}
		return false;
	}

	public void refresh() {
		this.getHandler().post(new Runnable() {
			public void run() {
				loadItems();
			}
		});
	}

	@Override
	protected boolean validate() {
		boolean success = true;
		if (this.subjectField.getText().toString().equals("")) {
			this.subjectField.setError(getString(R.string.ErrorRequired));
			success = false;
		}
		if (this.contentField.getText().toString().equals("")) {
			this.contentField.setError(getString(R.string.ErrorRequired));
			success = false;
		}
		return success;
	}

	private void loadItems() {
		List<Employee> employees = getEmployees();
		if (employees != null) {
			recipientAdapter = new EmployeeAdapter(this,
					R.layout.select_list_item, employees);
			recipientSpinner.setAdapter(recipientAdapter);
			this.fixSpinner(recipientSpinner);
		} else {
			MessageBox.show(this, getString(R.string.ErrorEmployeeList));
			finish();
		}
	}

	private List<Employee> getEmployees() {
		EmployeeManager manager = new EmployeeManager(this);
		List<Employee> employees = manager.GetEmployeeList();
		manager.Close();
		return employees;
	}

}
